﻿Imports System.ComponentModel

<ToolboxBitmap(GetType(System.Windows.Forms.ComboBox)), DefaultEvent("SelectedIndexChanged")> _
Public Class TerritoryBoxCtrl

#Region "Properties"

    Private _ConnectionString As String
    <Browsable(True), DefaultValue(""), Description("Connection string")> _
    Public Property zConnectionString() As String
        Get
            Return _ConnectionString
        End Get
        Set(ByVal value As String)
            _ConnectionString = value
            Connect()
        End Set
    End Property

    <Browsable(True), DefaultValue(""), Description("Territory Box text")> _
    Public Property zText() As String
        Get
            Return TheBox.Text
        End Get
        Set(ByVal value As String)
            TheBox.Text = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("Territory Box current selected Territory_Code")> _
    Public Property zSelectedTerritory_Code() As String

        Get
            Return TheBox.SelectedValue
        End Get
        Set(ByVal value As String)
            TheBox.SelectedValue = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("Territory Box current selected Index")> _
    Public Property SelectedIndex() As Integer

        Get
            Return TheBox.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            TheBox.SelectedIndex = value
        End Set
    End Property

    <Browsable(False), DefaultValue(""), Description("Territory Box current selected Territory_Name")> _
    Public ReadOnly Property zSelectedTerritory_Name() As String
        Get
            Return DirectCast(TheBox.SelectedItem, DataRowView).Item("Territory_Name")
        End Get
    End Property

    Private _AllOptionAvailable As Boolean
    <Browsable(True), DefaultValue(False), Description("Territory Box <All> option available")> _
    Public Property zAllOptionAvailable() As Boolean
        Get
            Return _AllOptionAvailable
        End Get
        Set(ByVal value As Boolean)
            _AllOptionAvailable = value
            Connect()
        End Set
    End Property


#End Region

#Region "Events"

    Public Custom Event SelectedIndexChanged As EventHandler
        AddHandler(ByVal value As EventHandler)
            AddHandler _TheBox.SelectedIndexChanged, value

        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler _TheBox.SelectedIndexChanged, value

        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

        End RaiseEvent
    End Event

#End Region

#Region "Functions"

    Private Function Connect() As Boolean

        If _ConnectionString = "" Then
            Return False
        End If

        Dim sql As String = ""

        If _AllOptionAvailable = True Then
            sql = "select '' as Territory_Code, '<All>' as Territory_Name from dbo.Territories UNION "
        End If
        sql = sql & "select Territory_Code, Territory_Name from dbo.Territories "

        sql = sql & "Order by Territory_Name"

        Dim success As Boolean = True
        Dim dv As DataView

        dv = LoadDataView(sql, _ConnectionString, success)

        If success Then
            '  TheBox.Items.Clear()
            TheBox.DisplayMember = "Territory_Name"
            TheBox.ValueMember = "Territory_Code"
            TheBox.DataSource = dv
            Return True
        Else
            Return False
        End If

    End Function

#End Region

End Class
